package com.ls.x.tool;

import java.io.File;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
	// picture_path,picture_note,voice_path,voice_note,
	final String[] CREATE_TABLE_SQL = {
			"create table context(id long primary key, user_id , title,summary,level,start_date,end_date,repeat,class,position,mode,status,remark,postpone_date,postpone_interval)",
			"create table image(id integer primary key AUTOINCREMENT,image_path,image_note,context_id)",
			"create table voice(id integer primary key AUTOINCREMENT,voice_path,voice_note,context_id)",
			"create table share(id long primary key,context_id,user_id,share_to_user_id,context_share_mark,sms_share_mark,email_share_mark)",
			"create table user(id long primary key,username,password,email,phone_number,user_group_id)",
			"create table user_group(id long primary key,user_id,groupname,description)",
			"create table selected_items(id long primary key,type_id,items_name)",
			"create table selected_types(id long primary key,name,description)",
			"create table remind_mode(id integer primary key AUTOINCREMENT,name,mode,remind_times,sms_number,alert_content)" };

	/**
	 * @param context
	 * @param name
	 * @param version
	 */
	public DatabaseHelper(Context context, String name, int version) {
		super(context, name, null, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// 第一次使用数据库时自动建表
		Log.d("lijing", "如果数据库不存在，则进入DatabaseHelper的onCreate()方法");
		for (String SQL : CREATE_TABLE_SQL) {
			db.execSQL(SQL);
			Log.d("lijing", "执行SQL语句：" + SQL);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	public static DatabaseHelper getDBhelper(Context context) {
		String path = Environment.getExternalStorageDirectory()
				+ "/.xProject/db/";
		File dir = new File(path);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		return new DatabaseHelper(context, path + "/xPorjeceDB.db3", 1);
	}
}
